import { Client } from 'minio';

const minioClient = new Client({
  endPoint: process.env.MINIO_ENDPOINT || 'localhost',
  port: parseInt(process.env.MINIO_PORT || '9000'),
  useSSL: process.env.MINIO_USE_SSL === 'true',
  accessKey: process.env.MINIO_ACCESS_KEY || 'minioadmin',
  secretKey: process.env.MINIO_SECRET_KEY || 'minioadmin',
});

// 默认存储桶名称
export const DEFAULT_BUCKET = process.env.MINIO_BUCKET || 'qa-express';

// 初始化存储桶
export const initializeBucket = async (): Promise<void> => {
  try {
    const bucketExists = await minioClient.bucketExists(DEFAULT_BUCKET);
    if (!bucketExists) {
      await minioClient.makeBucket(DEFAULT_BUCKET, 'us-east-1');
      console.log(`Bucket '${DEFAULT_BUCKET}' created successfully`);
    } else {
      console.log(`Bucket '${DEFAULT_BUCKET}' already exists`);
    }
  } catch (error) {
    console.error('Error initializing MinIO bucket:', error);
  }
};

export { minioClient };